package com.waner.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.waner.reggie.common.BaseContext;
import com.waner.reggie.common.R;
import com.waner.reggie.entity.Dish;
import com.waner.reggie.entity.Setmeal;
import com.waner.reggie.entity.ShoppingCart;
import com.waner.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 根据用户id查找菜品
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        // select * from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                .orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(wrapper);

        return R.success(list);
    }

    /**
     * 购物车中增加菜品
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("增加菜品操作...");

        // select * from shopping_cart where userId = ? or dishId = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId,shoppingCart.getSetmealId())
                .eq(shoppingCart.getDishFlavor() != null,ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor())
                .eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        if(cart!=null){
            // 直接在原来的基础上+1
            cart.setNumber(cart.getNumber()+1);
            shoppingCartService.updateById(cart);
            return R.success(cart);
        }else {
            // 新增一个订单
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("减少菜品操作..");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId())
                .eq(shoppingCart.getDishId() != null ,ShoppingCart::getDishId,shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart old = shoppingCartService.getOne(queryWrapper);

        // 减少数目
        old.setNumber(old.getNumber()-1);
        shoppingCartService.updateById(old);
        // 直接删除
        if(old.getNumber()==0) shoppingCartService.removeById(old);

        return R.success(old);
    }

    /**
     * 根据userId清空对应的购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);
        return R.success("清空成功！");
    }

}
